package com.itheima.utils;

import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.vo.UserVo;

import java.util.*;

public class UserVoUtils {

    public static List<UserVo> getUserVo(User byUsername) {

        Set<Menu> set = new LinkedHashSet<>();
        for (Role role : byUsername.getRoles()) {
            LinkedHashSet<Menu> menus = role.getMenus();
            for (Menu menu : menus) {
                set.add(menu);
            }
        }
        List<Menu> list = new ArrayList<>();
        List<Menu> list2 = new ArrayList<>();
        for (Menu menu : set) {
            if (menu.getParentMenuId()!=null) {
                list2.add(menu);
            } else {
                list.add(menu);
            }
        }
        ArrayList<UserVo> userVos1 = new ArrayList<>();
            for (Menu menu1 : list) {
                UserVo userVo = new UserVo(menu1.getPath(), menu1.getName(), menu1.getIcon(),menu1.getLinkUrl(),new ArrayList<UserVo>(),menu1.getPriority());
                for (Menu menu : list2) {
                    if (menu1.getId() == menu.getParentMenuId()) {
                        UserVo userVo2 = new UserVo(menu.getPath(), menu.getName(), menu.getIcon(),menu.getLinkUrl(),menu.getPriority());
                        userVo.getChildren().add(userVo2);
                    }
                }
                userVo.getChildren().sort(new Comparator<UserVo>() {
                    @Override
                    public int compare(UserVo o1, UserVo o2) {
                        return o1.getPriority()-o2.getPriority();
                    }
                });
                userVos1.add(userVo);
            }

            userVos1.sort(new Comparator<UserVo>() {
                @Override
                public int compare(UserVo o1, UserVo o2) {
                    return o1.getPriority()-o2.getPriority();
                }
            });
        return userVos1;


    }
    public static List<Menu> getMenu(List<Menu> menus) {
        List<Menu> list1 = new ArrayList<>();
        List<Menu> list2 = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentMenuId()!=null) {
                list2.add(menu);
            } else {
                list1.add(menu);
            }
        }
        System.out.println(list2);
        for (Menu menu : list1) {
            for (Menu menu1 : list2) {
                if(menu.getId()==menu1.getParentMenuId()){
                    menu.getChildren().add(menu1);
                }
            }
        }
        return list1;
    }


}
